//
//  Stop.m
//  Together_iOS
//
//  Created by applex on 14-3-1.
//  Copyright (c) 2014年 QIU Shuang. All rights reserved.
//

#import "Stop.h"
#import "Plan.h"


@implementation Stop

@dynamic remoteID;
@dynamic business_url;
@dynamic categories;
@dynamic has_online_reservation;
@dynamic latitude;
@dynamic longtitude;
@dynamic name;
@dynamic online_reservation_url;
@dynamic product_score;
@dynamic regions;
@dynamic s_photo_url;
@dynamic startTime;
@dynamic status;
@dynamic telephone;
@dynamic type;
@dynamic plan;
@dynamic business_id;


- (BOOL)isHas_online_reservation {
    return [self.has_online_reservation intValue] == 1;
}

#pragma mark - SSRemoteManagedObject

- (void)unpackDictionary:(NSDictionary *)dictionary {
    [super unpackDictionary:dictionary];
    self.business_url = dictionary[@"business_url"];
    self.categories = dictionary[@"categories"];
    self.has_online_reservation = @([dictionary[@"has_online_reservation"] intValue] == 1);
    self.latitude = @([dictionary[@"latitude"] floatValue]);
    self.longtitude = @([dictionary[@"longitude"] floatValue]);
    self.name = dictionary[@"name"];
    self.online_reservation_url = dictionary[@"online_reservation_url"];
    self.product_score = @([dictionary[@"product_score"] floatValue]);
    self.regions = dictionary[@"regions"];
    self.s_photo_url = dictionary[@"s_photo_url"];
    self.startTime = [NSDate dateWithTimeIntervalSince1970:[dictionary[@"start_time"] longLongValue] / 1000];
    self.status = @([dictionary[@"status"] intValue]);
    self.telephone = dictionary[@"telephone"];
    self.type = @([dictionary[@"type"] intValue]);
    self.business_id = dictionary[@"business_id"];
    
    if (dictionary[@"plan"] != nil) {
        self.plan = dictionary[@"plan"];
    } else if ([dictionary[@"plan_id"] intValue] != 0) {
        self.plan = [Plan objectWithRemoteID:@([dictionary[@"plan_id"] integerValue])];
    }
}


#pragma mark - SSManagedObject

+ (NSArray *)defaultSortDescriptors {
    return [NSArray arrayWithObjects:
            [NSSortDescriptor sortDescriptorWithKey:@"plan" ascending:NO],
            [NSSortDescriptor sortDescriptorWithKey:@"startTime" ascending:YES],
            nil];
}



- (NSDictionary *)packedDictionary {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (self.remoteID && [self.remoteID intValue] != 0) {
        [dic setObject:self.remoteID forKey:@"id"];
    }
    if (self.startTime) {
        [dic setObject:@((long long int)([self.startTime timeIntervalSince1970] * 1000)) forKey:@"start_time"];
    }
    if (self.business_url) {
        [dic setObject:self.business_url forKey:@"business_url"];
    }
    if (self.categories) {
        [dic setObject:self.categories forKey:@"categories"];
    }
    if (self.has_online_reservation) {
        [dic setObject:self.has_online_reservation forKey:@"has_online_reservation"];
    }
    if (self.latitude) {
        [dic setObject:self.latitude forKey:@"latitude"];
    }
    if (self.longtitude) {
        [dic setObject:self.longtitude forKey:@"longitude"];
    }
    if (self.name) {
        [dic setObject:self.name forKey:@"name"];
    }
    if (self.online_reservation_url) {
        [dic setObject:self.online_reservation_url forKey:@"online_reservation_url"];
    }
    if (self.product_score) {
        [dic setObject:self.product_score forKey:@"product_score"];
    }
    if (self.regions) {
        [dic setObject:self.regions forKey:@"regions"];
    }
    if (self.s_photo_url) {
        [dic setObject:self.s_photo_url forKey:@"s_photo_url"];
    }
    if (self.status) {
        [dic setObject:self.status forKey:@"status"];
    }
    if (self.telephone) {
        [dic setObject:self.telephone forKey:@"telephone"];
    }
    if (self.type) {
        [dic setObject:self.type forKey:@"type"];
    }
    if (self.plan) {
        [dic setObject:self.plan.remoteID forKey:@"plan_id"];
    }
    return dic;
}


+ (id)objectWithRemoteID:(NSNumber *)remoteID context:(NSManagedObjectContext *)context {
    
	// If there isn't a suitable remoteID, we won't find the object. Return nil.
	if (!remoteID ||
		![remoteID respondsToSelector:@selector(integerValue)]) {
		return nil;
	}
    
	// Default to the main context
	if (!context) {
		context = [self mainQueueContext];
	}
    
	// Look up the object
	SSRemoteManagedObject *object = [self existingObjectWithRemoteID:remoteID context:context];
    
	// If the object doesn't exist, create it
	if (!object) {
		object = [[self alloc] initWithContext:context];
		object.remoteID = remoteID;
	}
    
	// Return the fetched or new object
	return object;
}


@end
